﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Serialization;
using System.Linq;

namespace FormDesigner
{
    public class DataTable
    {
        public DataTable()
        {
            Rows = new List<List<Cell>>();
            ListCellWidth = new List<int>();
            ListCellHeight = new List<int>();
        }
        /// <summary>
        /// 深度复制表格
        /// </summary>
        /// <returns></returns>
        public DataTable DeepCopy()
        {
            byte[] json = null;
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DataTable));
            using (MemoryStream ms = new MemoryStream())
            {
                ser.WriteObject(ms, this);
                json = ms.ToArray();
            }
            string jsonString = Encoding.UTF8.GetString(json, 0, json.Length);//序列化得到的字符串 

            DataTable dt = null;
            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
            {
                dt = (DataTable)ser.ReadObject(ms);
            }
            return dt;

        }

        /// <summary>
        /// 表格每列的宽度
        /// </summary>
        public List<int> ListCellWidth
        {
            get;
            set;
        }
        /// <summary>
        /// 表格每行的高度
        /// </summary>
        public List<int> ListCellHeight
        {
            get;
            set;
        }
        /// <summary>
        /// List<List<Cell>>表示所有行，List<Cell>表示一行
        /// </summary>
        public List<List<Cell>> Rows
        {
            get;
            set;
        }
        /// <summary>
        /// 表格的列
        /// </summary>
        public List<Cell> Columns
        {
            get
            {
                if (Rows.Count == 0)
                    return new List<Cell>();
                else
                    return Rows[0];
            }
            set { }
        }
        /// <summary>
        /// 表格的所有单元格
        /// </summary>
        public List<Cell> AllCell
        {
            get
            {
                List<Cell> temp = new List<Cell>();
                foreach (var list in Rows)
                {
                    temp.AddRange(list);
                }
                return temp;
            }
            set { }
        }
    }
}
